package mysql

import (
	"videos/videorpc/videorpc"

	"gorm.io/gorm"
)

// 评论

type Comment struct {
	gorm.Model
	Content string `gorm:"type:varchar(30);not null;comment:评论内容"`
	VideoId int    `gorm:"type:int;not null;comment:视频ID"`
	UserId  int    `gorm:"type:int;not null;comment:用户ID"`
}

// 评论用户

type CommentUser struct {
	CommentId int
	Content   string
	CreatedAt string
	NickName  string
}

// 评论用户展示

func (u *CommentUser) CommentShow(db *gorm.DB, id int64) error {
	/*
		select
		comments.id as CommentId,
		comments.content as content,
		comments.created_at as `comments.created_at`,
		users.nick_name as nick_name
		from comments
		left join users on users.id = comments.user_id
	*/
	return db.Model(&Comment{}).
		Select(
			"comments.id as CommentId",
			"comments.content as content",
			"comments.created_at as `comments.created_at`",
			"users.nick_name as nick_name",
		).
		Joins("left join users on users.id = comments.user_id").
		Debug().Where("comments.id = ?", id).Find(u).Error
}

// 评论添加

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

// 评论删除

func (c *Comment) CommentDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(c).Error
}

// 评论列表

func CommentList(db *gorm.DB, in *videorpc.CommentListRequest) []*videorpc.CommentList {
	/*
		select
		comments.id as CommentId,
		comments.content as content,
		users.nick_name as nick_name,
		users.head_image as head_image,
		videos.title as title,
		videos.style as style
		from comments
		left join users on users.id = comments.user_id
		left join videos on videos.id = comments.video_id
	*/
	var list []*videorpc.CommentList

	db.Model(&Comment{}).
		Select(
			"comments.id as CommentId",
			"comments.content as content",
			"comments.created_at",
			"users.nick_name as nick_name",
			"users.head_image as head_image",
			"videos.title as title",
			"videos.style as style",
		).
		Joins("left join users on users.id = comments.user_id").
		Joins("left join videos on videos.id = comments.video_id").
		Debug().Scopes(Paginate(int(in.Page), int(in.Size))).Order("comments.created_at desc").Find(&list)

	return list
}
